package com.htc.callcare.asyntask;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import com.htc.callcare.infrastructure.globalVar;
import com.htc.callcare.model.recordModel;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class AsynLoadAllRecordFile extends AsyncTask<String, Integer, ArrayList<recordModel>>{
	
	Context context;
	ProgressDialog progressDialog;
	String progressMessage;
	

	public AsynLoadAllRecordFile(Context context, String processMessage) {
		super();
		this.context = context;
		this.progressMessage = processMessage;
	}

		@Override
	protected void onPreExecute() {
		
		progressDialog = ProgressDialog
				.show(context, "", progressMessage, true);
	}

	@Override
	protected void onPostExecute(ArrayList<recordModel> result) {
		// TODO Auto-generated method stub
		progressDialog.dismiss();
	}


	@Override
	protected ArrayList<recordModel> doInBackground(String... params)/*
																	 * throws
																	 * Exception
																	 */{
		// TODO Auto-generated method stub
		ArrayList<recordModel> list = new ArrayList<recordModel>();
		File root = new File(globalVar.recordDir);

		{
			try {
				if (root.listFiles().length > 0) {
					for (File file : root.listFiles()) {
						if (file.getName().endsWith(globalVar.amrType)) {
							recordModel record = new recordModel(file.getName());
							list.add(record);
						} else if (file.getName().endsWith(globalVar.mp4Type)) {
							recordModel record = new recordModel(file.getName());
							list.add(record);
						} else if (file.getName().endsWith(
								globalVar.three_gppType)) {
							recordModel record = new recordModel(file.getName());
							list.add(record);
						}
					}
				} else {
				}
			} catch (Exception e) {
			}
		}
		if (list.size() != 0) {
			return newList(sortList(list));
		} else {
			return list;
		}
	}
	
	public ArrayList<recordModel> sortList(ArrayList<recordModel> records) {
		Collections.sort(records);
		return records;
	}

	public ArrayList<recordModel> newList(ArrayList<recordModel> records){
		records.add(0, new recordModel(records.get(0).getCallDay(), 1));
		for(int i = 0;i<records.size();i++){
			if(i+1<records.size()){
				//Add separator
				if(!records.get(i).getCallDay().equals(records.get(i+1).getCallDay())){
					records.add(i+1, new recordModel(records.get(i+1).getCallDay(), 1));
				}
			}
		}
		return records;
	}
	/*class AMRFilter implements FilenameFilter {
		public boolean accept(File dir, String name) {
			return (name.endsWith(globalVar.amrType));
		}

	}
	
	class MP4Filter implements FilenameFilter {
		public boolean accept(File dir, String name) {
			return (name.endsWith(globalVar.mp4Type));
		}
	}
	
	class Three_GPPFilter implements FilenameFilter{

		@Override
		public boolean accept(File dir, String name) {
			return (name.endsWith(globalVar.three_gppType));
		}
		
	}*/

}
